Repository navigation
Conversation
zcbenz
reviewed
Aug 27, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Can you move the test to tests/test_tuner_trainer.py?
zcbenz
force-pushed
the
fix/qwen3-moe-routing-index-gradients
branch
2 times, most recently
from
August 28, 2026 09:24
766e5a7 to
603b9f6
Compare
zcbenz
approved these changes
Aug 28, 2026
zcbenz
force-pushed
the
fix/qwen3-moe-routing-index-gradients
branch
from
August 28, 2026 09:26
603b9f6 to
230fa6d
Compare
zcbenz
force-pushed
the
fix/qwen3-moe-routing-index-gradients
branch
from
August 28, 2026 09:26
230fa6d to
2d613f0
Compare
adityak74
added a commit
to adityak74/mlx-lm
that referenced
this pull request
Aug 29, 2026
Per review: match the test placement used by ml-explore#1787 instead of a separate test file. Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
adityak74
added a commit
to adityak74/mlx-lm
that referenced
this pull request
Aug 29, 2026
…ybrid, lfm2_moe
LoRA fine-tuning (mlx_lm.lora --train) crashes on these three model families
with:
ValueError: [gather_axis] Cannot calculate VJP with respect to indices.
The integer top-k expert indices derive from differentiable router logits and
flow into take_along_axis / the switch MLP, so backward propagation requests a
VJP w.r.t. the indices unless explicitly barriered. Most sibling MoE models
(olmoe, mixtral, qwen2_moe, phimoe, glm4_moe, ...) already wrap their routing
indices in mx.stop_gradient; these three were missing the guard.
Same fix as ml-explore#1787 (qwen3_moe), applied to the remaining affected families.
Adds a backward-pass regression test for all three.
Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
adityak74
added a commit
to adityak74/mlx-lm
that referenced
this pull request
Aug 29, 2026
Per review: match the test placement used by ml-explore#1787 instead of a separate test file. Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Stops gradients through the integer top-k expert indices produced in
Qwen3MoeSparseMoeBlock.Why
The indices derive from differentiable router scores and are consumed by
take_along_axisandSwitchGLU. Without an explicit barrier, LoRA backward propagation asks MLX for a VJP with respect to the indices and fails with:Integer expert selections are non-differentiable.
mx.stop_gradient(inds)preserves the forward computation while preventing the invalid gradient request. Router scores and model activations remain differentiable.Tests
Adds
tests/test_qwen3_moe_gradients.py, which constructs a small one-layer Qwen3 MoE model and verifies finite backward propagation:Validation completed on current
main:The change also completed one-step and twenty-step checkpointed LoRA smoke runs on
Qwen3-30B-A3B-Instruct-2507-MLX-4bit, including adapter save and reload. The longer run used 1,736-token disposable inputs.Fixes #1786
Related: ml-explore/mlx#3787, #1449, #1452